package com.a01_2021.thousandstimes.utils;

import org.springframework.util.StringUtils;

/**
 * @author E1xP@foxmail.com
 * @version 1.0
 * @PACKAGE_NAME com.a01_2021.thousandstimes.utils
 * @CLASS_NAME Desensitization
 * @Description TODO 用于数据脱敏传输
 * @Date 2021/4/1 上午 11:36
 **/
public class Desensitization {

    /**
     * 用于脱敏电话号码
     *
     * @param phoneNo 电话号码
     * @return String
     */
    public static String phoneNo(String phoneNo) {
        if (StringUtils.isEmpty(phoneNo))
            return phoneNo;
        String desensitisedPhoneNo;
        desensitisedPhoneNo = phoneNo.substring(0, (phoneNo.length() - 4) / 2);
        desensitisedPhoneNo += "*".repeat((phoneNo.length() - 4) / 2);
        desensitisedPhoneNo += phoneNo.substring(phoneNo.length() - 4);
        return desensitisedPhoneNo;
    }

    /**
     * 用于脱敏身份证
     *
     * @param personalNo 身份证号码
     * @return String
     */
    public static String personalNo(String personalNo) {
        if (StringUtils.isEmpty(personalNo)) {
            return personalNo;
        }
        String desensitisedPersonalNo;
        if (personalNo.length() < 10) {
            return "*".repeat(personalNo.length());
        }
        desensitisedPersonalNo = personalNo.substring(0, 6);
        desensitisedPersonalNo += "*".repeat(personalNo.length() - 4 - 6);
        desensitisedPersonalNo += personalNo.substring(personalNo.length() - 4);
        return desensitisedPersonalNo;
    }
}
